package map.android.baidu.carpool.utils;

import android.os.Handler;
import android.os.Message;

/**
 * Created by zhaoyangyang on 2015/4/3.
 */
public class CountDownTimerUtil {
    /**
     * 该类实现倒计时的功能
     * param:时间戳
     */
    private Long mTime = 10000L;
    private CompleteDownTime mDownTimeListener = null;

    public CountDownTimerUtil(Long delayTime) {
        this.mTime = delayTime;

    }

    public void setDownTimeListener(CompleteDownTime downTimeListener) {
        this.mDownTimeListener = downTimeListener;
    }

    public void startDownTime() {
        //TODO 开始倒计时
        Message message = handlerCountDown.obtainMessage(1);
        handlerCountDown.sendMessageDelayed(message, 1000);
    }

    final Handler handlerCountDown = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mTime = mTime - 1000;
                    if (mTime > 0) {
                        Message message = handlerCountDown.obtainMessage(1);
                        handlerCountDown.sendMessageDelayed(message, 1000);
                    } else {
                        if (mDownTimeListener != null) {
                            mDownTimeListener.downTimeComplete();
                        }

                    }
            }

            super.handleMessage(msg);
        }
    };

    public interface CompleteDownTime {
        public void downTimeComplete();
    }

}
